package com.cyt.web.controller.sys;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysOrganization;
import com.cyt.sys.model.vo.SysOrganizationVo;
import com.cyt.sys.service.SysOrganizationService;
import com.github.pagehelper.StringUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;


/**
 * @Author: liufu zhu
 * @Date: 2018-11-30 15:00
 * @Description:组织机构控制层
 */
@RestController
public class SysOrganizationController extends SysBaseController {

    @Resource
    private SysOrganizationService sysOrganizationService;


    /**
     * @author: liufu zhu
     * @date: 2018/11/30 15:01
     * @Description: 组织机构模块统一跳转处理
     */

    @RequestMapping("/sys/organization/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * @author: liufu zhu
     * @date: 2018/11/30 15:30
     * @Description: 组织机构list查询
     */
    @BaseLog(module = "系统模块", classz = "组织机构管理", methods = "组织机构List", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysOrganization/list", method = RequestMethod.POST)
    public ReturnResult list(SysOrganizationVo sysOrganizationVo) {
        ReturnResult returnResult = sysOrganizationService.list(sysOrganizationVo);
        return returnResult;
    }

    /**
     * @author: liufu zhu
     * @date: 2018/11/30 17:47
     * @Description: 组织机构删除
     */
    @BaseLog(module = "系统模块", classz = "组织机构管理", methods = "组织机构删除", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysOrganization/delete", method = RequestMethod.GET)
    public ReturnResult delete(String id) {
        return sysOrganizationService.delete(id);
    }

    /**
     * @author: liufu zhu
     * @date: 2018/12/1 8:17
     * @Description: 添加页面跳转
     */
    @RequestMapping(value = "/sysOrganization/getAdd", method = RequestMethod.GET)
    public ModelAndView getAdd(String id, String parentId) {
        ModelAndView view = new ModelAndView("/sys/organization/organizationAdd");
        if (id != null && !"".equals(id)) {
            SysOrganization sysOrganization = sysOrganizationService.selectByPrimaryKey(id);
            view.addObject(sysOrganization);
        }
        if (StringUtil.isNotEmpty(parentId)) {
            SysOrganization sysOrganization = new SysOrganization();
            sysOrganization.setParentId(parentId);
            view.addObject(sysOrganization);
        }
        return view;
    }

    /**
     * @author: liufu zhu
     * @date: 2018/12/1 8:19
     * @Description: 编辑页面跳转
     */
    @RequestMapping(value = "/sysOrganization/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String parentId) {
        ModelAndView view = new ModelAndView("/sys/organization/organizationEdit");
        if (id != null && !"".equals(id)) {
            SysOrganization sysOrganization = sysOrganizationService.selectById(id);
            view.addObject(sysOrganization);
        }
        if (StringUtil.isNotEmpty(parentId)) {
            SysOrganization sysOrganization = new SysOrganization();
            sysOrganization.setParentId(parentId);
            view.addObject(sysOrganization);
        }
        return view;
    }

    /**
     * @author: liufu zhu
     * @date: 2018/12/1 9:07
     * @Description: 保存/更新组织机构
     */
    @BaseLog(module = "系统模块", classz = "组织机构管理", methods = "保存/更新组织机构", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysOrganization/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(SysOrganizationVo sysOrganizationVo) {
        ReturnResult baseOrganization = sysOrganizationService.addOrUpdate(sysOrganizationVo);
        return baseOrganization;

    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-05 16:17
     * @Description:获取组织机构树
     */
    @BaseLog(module = "系统模块", classz = "组织机构管理", methods = "组织机构树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysOrganization/dTreeList", method = RequestMethod.POST)
    public ReturnResult dTreeList(SysOrganizationVo sysOrganizationVo) {
        return sysOrganizationService.dTreeList(sysOrganizationVo);
    }
}
